<?php

namespace App\Http\Controllers\Admin;

use App\Models\CopartnerApply;
use App\Models\RechargeCommission;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CopartnerController extends Controller
{
    public function apply()
    {
        $list = CopartnerApply::query()
            ->select('user.user_name','copartner_applies.*')
            ->leftJoin('user','user.id','=','copartner_applies.user_id')
            ->get()
            ->toArray();
        $total_recharge = 0;
        foreach($list as &$v){
            $recharge_money = RechargeCommission::query()->groupBy('from_uid')->where('pay_status',2)->sum('total_amount');
            $total_recharge += $recharge_money;
            $v['total_recharge'] = $recharge_money;
        }
        $data = [
            'list' => $list,
            'total_recharge' => $total_recharge
        ];
        return view('admin.copartner.apply',$data);
    }

    public function doapply(Request $request,$apply_id,$status)
    {
        $model = CopartnerApply::find($apply_id);
        if(!$model){
            return redirect('admin/copartner/apply')->with('danger','请选择你要同意的申请项');
        }

        if($model -> status != 0){
            return redirect('admin/copartner/apply')->with('danger','该申请已经处理过了，不能重复处理');
        }

        $model -> status = $status;
        if($model -> save()){
            return redirect('admin/copartner/apply')->with('success','处理成功');
        }

        return redirect('admin/copartner/apply')->with('danger','处理失败');
    }
}
